package com.sky.controller.common;

import com.sky.result.Result;
import com.sky.service.common.CommonService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@RestController
public class CommonController {
    @Resource
    private CommonService commonService;

    //短信发送接口
    @GetMapping("/courier/validateCodeToPhone")
    public Result sendMessageToPhone(String telephone) {
        return commonService.sendMessage(telephone);
    }

    /**
     * rider.html查询位置
     */
    @GetMapping("/dispatch/loadRouteInfo")
    public Result loadRouteInfo(Long tradeNo, Long courierId, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime expire) {
        return commonService.loadRouteInfo(tradeNo, courierId, expire);
    }
    /**
     * 解析短链接口
     */
    @GetMapping("/r/{code}")
    public void shortUrl(@PathVariable("code") String code, HttpServletResponse response){
        //1.查询完整链接
        String url =commonService.findShortUrl(code);
        try {
            //重定向
            //将连接发送给用户
            response.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
